package cn.wj.school.common.dto;

import cn.wj.school.common.Constants;
import cn.wj.school.common.enums.MsgType;
import io.netty.buffer.ByteBuf;
import lombok.Data;
import org.apache.commons.lang.StringUtils;

@Data
public abstract  class CommonMsg extends BaseMsg {


    /**
     * 操作是否成功
     */
    private byte success = Constants.TRUE;

    private String error;


    @Override
    public abstract MsgType getMsgType();


    @Override
    public void writeBody(ByteBuf buf) {
        buf.writeByte(success);
        if (StringUtils.isNotBlank(error)) {
            writeUTF8(buf, error);
        }
    }

    @Override
    public void readBody(ByteBuf buf) {
        this.success = buf.readByte();
        if (this.success == Constants.FALSE) {
            this.error = readUTF8(buf);
        }
    }
}
